package com.chan.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.chan.common.core.text.Convert;
import com.chan.common.utils.DateUtils;
import com.chan.domain.CdBatchFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chan.mapper.CdProgramFileMapper;
import com.chan.domain.CdProgramFile;
import com.chan.service.ICdProgramFileService;


/**
 * 附件Service业务层处理
 *
 * @author chan
 * @date 2024-04-30
 */
@Service
public class CdProgramFileServiceImpl implements ICdProgramFileService
{
    @Autowired
    private CdProgramFileMapper cdProgramFileMapper;

    /**
     * 查询附件
     *
     * @param id 附件主键
     * @return 附件
     */
    @Override
    public CdProgramFile selectCdProgramFileById(Long id)
    {
        return cdProgramFileMapper.selectCdProgramFileById(id);
    }

    /**
     * 查询附件列表
     *
     * @param cdProgramFile 附件
     * @return 附件
     */
    @Override
    public List<CdProgramFile> selectCdProgramFileList(CdProgramFile cdProgramFile)
    {
        return cdProgramFileMapper.selectCdProgramFileList(cdProgramFile);
    }

    /**
     * 新增附件
     *
     * @param cdProgramFile 附件
     * @return 结果
     */
    @Override
    public int insertCdProgramFile(CdProgramFile cdProgramFile)
    {
        cdProgramFile.setCreateTime(DateUtils.getNowDate());
        return cdProgramFileMapper.insertCdProgramFile(cdProgramFile);
    }

    /**
     * 修改附件
     *
     * @param cdProgramFile 附件
     * @return 结果
     */
    @Override
    public int updateCdProgramFile(CdProgramFile cdProgramFile)
    {
        cdProgramFile.setUpdateTime(DateUtils.getNowDate());
        return cdProgramFileMapper.updateCdProgramFile(cdProgramFile);
    }

    /**
     * 批量删除附件
     *
     * @param ids 需要删除的附件主键
     * @return 结果
     */
    @Override
    public int deleteCdProgramFileByIds(String ids)
    {
        return cdProgramFileMapper.deleteCdProgramFileByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除附件信息
     *
     * @param id 附件主键
     * @return 结果
     */
    @Override
    public int deleteCdProgramFileById(Long id)
    {
        return cdProgramFileMapper.deleteCdProgramFileById(id);
    }


    @Override
    public int batchInsertCdProgramFile(CdBatchFile cdBatchFile) {
        /***
         * return urls：http://localhost:8089/profile/upload/2024/04/30/各用户名密码 _20240430162525A001.xls,http://localhost:8089/profile/upload/2024/04/30/即时聊天_20240430162525A002.docx
         * common.js?v=4.7.8:425 reutrn fileNames：/profile/upload/2024/04/30/各用户名密码 _20240430162525A001.xls,/profile/upload/2024/04/30/即时聊天_20240430162525A002.docx
         * common.js?v=4.7.8:425 reutrn newFileNames：各用户名密码 _20240430162525A001.xls,即时聊天_20240430162525A002.docx
         * common.js?v=4.7.8:425 return originalFilenames：各用户名密码 .xls,即时聊天.docx
         */
        String fileNames = cdBatchFile.getFileNames();
        String[] filePathList = fileNames.split(",");
        String originalFilenames = cdBatchFile.getOriginalFilenames();
        String[] originalFilenameList = originalFilenames.split(",");
        String newFileNames = cdBatchFile.getNewFileNames();
        String[] fileNameList = newFileNames.split(",");
        List<CdProgramFile> fileList = new ArrayList<>();
        CdProgramFile cdProgramFile = null;
        for (int i=0 ; i<fileNameList.length ; i++) {
            cdProgramFile = new CdProgramFile();
            cdProgramFile.setFileName(fileNameList[i]);
            cdProgramFile.setFilePath(filePathList[i]);
            cdProgramFile.setOriginalFilename(originalFilenameList[i]);
            cdProgramFile.setCreateTime(DateUtils.getNowDate());
            cdProgramFile.setRelateId(cdBatchFile.getRelateId());
            cdProgramFile.setRelateMode(cdBatchFile.getRelateMode());
            fileList.add(cdProgramFile);
        }
        return cdProgramFileMapper.batchInsertCdProgramFile(fileList);
    }
}
